<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class b2c_ctl_admin_member_shenhe extends desktop_controller{

    var $workground = 'b2c_ctl_admin_member';
    var $member_model ;

    public function __construct($app)
    {
        parent::__construct($app);
        $this->member_model = $this->app->model('member_shenhe');
        header("cache-control: no-store, no-cache, must-revalidate");
    }

    function index(){

        $custom_actions[] =  array('label'=>app::get('b2c')->_('审核通过'),'submit'=>'index.php?app=b2c&ctl=admin_member_shenhe&act=allow');
        $custom_actions[] =  array('label'=>app::get('b2c')->_('审核拒绝'),'submit'=>'index.php?app=b2c&ctl=admin_member_shenhe&act=refuse');

        $actions_base['title'] = app::get('b2c')->_('预存款审核');
        $actions_base['actions'] = $custom_actions;
        $actions_base['use_buildin_recycle'] = false;
        $this->finder('b2c_mdl_member_shenhe',$actions_base);
    }

     /**
     * 桌面订单相信汇总显示
     * @param null
     * @return null
     */
    public function _views(){
        $obj_shenhe = $this->app->model('member_shenhe');
        $sub_menu = array(
            1=>array('label'=>app::get('b2c')->_('审核中'),'optional'=>false,'filter'=>array('shenhe_state'=>0,'disabled'=>'false')),
            2=>array('label'=>app::get('b2c')->_('审核完成'),'optional'=>false,'filter'=>array('shenhe_state'=>1,'disabled'=>'false')),
            3=>array('label'=>app::get('b2c')->_('审核拒绝'),'optional'=>false,'filter'=>array('shenhe_state'=>2,'disabled'=>'false')),
            4=>array('label'=>app::get('b2c')->_('全部'),'optional'=>false,'filter'=>array('disabled'=>'false')),
        );

        if(isset($_GET['optional_view'])) $sub_menu[$_GET['optional_view']]['optional'] = false;

        foreach($sub_menu as $k=>$v){
            if($v['optional']==false){
                $show_menu[$k] = $v;
                if(is_array($v['filter'])){
                    $v['filter'] = array_merge(array(),$v['filter']);
                }else{
                    $v['filter'] = array();
                }
                $show_menu[$k]['filter'] = $v['filter']?$v['filter']:null;
                if($k==$_GET['view']){
                    $show_menu[$k]['newcount'] = true;
                    $show_menu[$k]['addon'] = $obj_shenhe->count($v['filter']);
                }
                $show_menu[$k]['href'] = 'index.php?app=b2c&ctl=admin_member_shenhe&act=index&view='.($k).(isset($_GET['optional_view'])?'&optional_view='.$_GET['optional_view'].'&view_from=dashboard':'');
            }elseif(($_GET['view_from']=='dashboard')&&$k==$_GET['view']){
                $show_menu[$k] = $v;
            }
        }

        return $show_menu;
    }

    function allow(){
        $obj = new desktop_user();
        $operator = ($obj->get_login_name())?($obj->get_login_name()):'system_core';
        if($_POST['shenhe_id']){
            $obj_shenhe = app::get('b2c')->model('member_shenhe');
            $adjmember = &$this->app->model('members');
            $mem_adv = &$this->app->model('member_advance');
            $aShenhe = $_POST['shenhe_id'];
            $shenhe_id=implode(',',$aShenhe);
            $aList = array();
            foreach( $aShenhe as &$sid){
              $aList[] =array_values($obj_shenhe->getList('*',array('shenhe_id'=>$sid)));
            }
            foreach ($aList as &$v) {
                if($v[0]['shenhe_state']){
                    $this->splash('error','','已经审核过不能再次审核！');
                    exit;
                }
                $member_id=$v[0]['member_id'];
                $data=array(
                    'member_id'=>$v[0]['member_id'],
                    'modify_advance'=>$v[0]['modify_advance'],
                    'modify_advance_type'=>$v[0]['modify_advance_type'],
                    'modify_bank_name'=>$v[0]['modify_bank_name'],
                    'modify_memo'=>$v[0]['modify_memo'],
                    );
                // $res = $obj_shenhe->db->exec('UPDATE sdb_b2c_member_shenhe SET shenhe_state = "1" where shenhe_id IN ('.(implode(',',$aShenhe)).')');
                // $db = kernel::database();
                // $sql="SELECT * from sdb_b2c_member_shenhe where shenhe_id='{$v[0]['shenhe_id']}'";
                // $shenhes=$db->select($sql);
                // dump($sql);dump($shenhes);die;
                if($v[0]['shenhe_state']==0){
                 $obj_shenhe -> update(array('shenhe_state'=>1,'op_time'=>time(),'operator'=>$operator),array('shenhe_id'=>$v[0]['shenhe_id']));
                }
               
                // $res = $obj_shenhe->db->exec('UPDATE sdb_b2c_member_shenhe SET shenhe_state = "1" where shenhe_id = '.$v[0]['shenhe_id']);
                // dump($data);die;
                
                if(!$mem_adv->adj_amount($v[0]['member_id'],$data,$msg,false)){
                    header('Content-Type:text/jcmd; charset=utf-8');
                    echo '{error:"'.$msg.'",_:null}';
                    $res = $obj_shenhe->db->exec('UPDATE sdb_b2c_member_shenhe SET shenhe_state = "0" where shenhe_id = '.$v[0]['shenhe_id']);
                }
                #↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓记录管理员操作日志@lujy↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
                if($obj_operatorlogs = kernel::service('operatorlog.members')){
                    if(method_exists($obj_operatorlogs,'detail_advance_log')){
                        $newdata = app::get('b2c')->model('members')->dump($member_id);
                        $obj_operatorlogs->detail_advance_log($newdata,$olddata);
                    }
                }

            }
            $url = "?app=b2c&ctl=admin_member_shenhe&act=index";
            $this->splash('success',$url,'审核成功！');
        }
    }

    function refuse(){
        $obj = new desktop_user();
        $operator = ($obj->get_login_name())?($obj->get_login_name()):'system_core';
        $obj_shenhe = app::get('b2c')->model('member_shenhe');
        $adjmember = &$this->app->model('members');
        $mem_adv = &$this->app->model('member_advance');
        $aShenhe = $_POST['shenhe_id'];
        $shenhe_id=implode(',',$aShenhe);
        $aList = array();
        foreach( $aShenhe as &$sid){
          $aList[] =array_values($obj_shenhe->getList('*',array('shenhe_id'=>$sid)));
        }

        foreach ($aList as &$v) {
            if($v[0]['shenhe_state']){
                $this->splash('error','','已经审核过不能再次审核！');
                exit;
            }
            $member_id=$v[0]['member_id'];
            $data[]=array(
                'modify_advance'=>$v[0]['modify_advance'],
                'modify_advance_type'=>$v[0]['modify_advance_type'],
                'modify_bank_name'=>$v[0]['modify_bank_name'],
                'modify_memo'=>$v[0]['modify_memo'],
            );
            // $res = $obj_shenhe->db->exec('UPDATE sdb_b2c_member_shenhe SET shenhe_state = "2" where shenhe_id = '.$v[0]['shenhe_id']);
            // if($v[0]['shenhe_id']==0){
                 $obj_shenhe -> update(array('shenhe_state'=>2,'op_time'=>time(),'operator'=>$operator),array('shenhe_id'=>$v[0]['shenhe_id']));
            // }
           
        }
        $url = "?app=b2c&ctl=admin_member_shenhe&act=index";
        $this->splash('success',$url,'拒绝审核成功！');
    }


}
